package com.jianglin.controller;

import com.github.pagehelper.PageInfo;
import com.jianglin.model.Carcharge;
import com.jianglin.model.Owner;
import com.jianglin.model.Parking;
import com.jianglin.service.ICarchargeService;
import com.jianglin.service.IOwnerService;
import com.jianglin.service.IParkingService;
import com.jianglin.util.JsonObject;
import com.jianglin.util.JwtUtil;
import com.jianglin.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 *
 *
 * @author liyijia
 */

@Api(tags = {"车辆收费管理"})
@RestController
@RequestMapping("/carcharge")
@SuppressWarnings("SpellCheckingInspection")

public class CarchargeController {

    @Resource
    private ICarchargeService carchargeService;

    @Resource
    private IParkingService parkingService;

    @Resource
    private IOwnerService ownerService;
    @Resource
    private  JwtUtil jwtUtil;


    @RequestMapping("/queryCarchargeAll")
    public JsonObject<Carcharge> queryCarchargeAll(
            @RequestParam(required = false) String numbers,
            @RequestParam(required = false) Integer status, // 修改为Integer类型
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer limit) {

        Carcharge carcharge = new Carcharge();

        // 处理车位号条件
        if (StringUtils.isNotBlank(numbers)) {
            Parking parking = new Parking();
            parking.setNumbers(numbers);
            carcharge.setParking(parking);
        }

        // 直接设置Integer类型的status
        carcharge.setStatus(status);

        PageInfo<Carcharge> pageInfo = carchargeService.findCarchargeAll(page, limit, carcharge);
        return new JsonObject<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }
    @RequestMapping("/queryCarchargeAll2")
    public JsonObject<Carcharge> queryCarchargeAll2(Carcharge carcharge, HttpServletRequest request,
                                         @RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "15") Integer limit) {

       //获取业主的token
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7).trim();
       //根据token获取业主名字
        String username = jwtUtil.getUsernameFromToken(token);
        // 根据username获取登录账号的业主id
        Owner owner = ownerService.queryOwnerByName(username);
        Integer userId = owner.getId();
        carcharge.setOwnerId(userId);
        PageInfo<Carcharge> pageInfo = carchargeService.findCarchargeAll(page, limit, carcharge);
        return new JsonObject<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }



@ApiOperation(value = "新增")
@RequestMapping("/initData")
public R InitData(@RequestBody Carcharge carcharge){
    if (carcharge.getMoney() == null) {
        //增加的金额不能为空
        return R.fail(400, "金额不能为空");
    }
    if (carcharge.getPayDate() == null || carcharge.getEndDate() == null) {
        //增加的日期不能为空
        return R.fail(400, "缴费日期和截止日期不能为空");
    }

    //遍历所有车位
    List<Parking> parkingList=parkingService.queryParkingByStatus();
    for(Parking park:parkingList){
        //默认设置车位费没有缴纳，状态0
        carcharge.setStatus(0);
        carcharge.setOwnerId(park.getOwnerId());
        carcharge.setType("停车费");
        carcharge.setParkId(park.getId());
        carchargeService.add(carcharge);
    }
    return R.ok();

}

    @ApiOperation(value = "删除")
    @RequestMapping("/deleteByIds")
    public R delete(String ids){
        String[] list= ids.split(",");
        //批量删除车位费
        for(String id:list){
            // 确保使用 Integer 类型
            Integer idInt = Integer.valueOf(id);
           Carcharge carcharge=carchargeService.findById(idInt);
           if(carcharge==null){
               return R.fail("该车位费不存在");
           }
           carchargeService.delete(idInt);


        }
        return R.ok();

    }

    @ApiOperation(value = "更新缴费状态")
    @RequestMapping("/update")
    public R update(@RequestParam Integer id){
        Carcharge carcharge =new Carcharge();
        carcharge.setId(id);
        //将缴费状态改为1
        carcharge.setStatus(1);

        int num=carchargeService.updateData(carcharge);
        if(num>0){
            return R.ok();
        }
        return R.fail("更新失败");
    }


    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码"),
            @ApiImplicitParam(name = "pageCount", value = "每页条数")
    })
    @GetMapping
    public JsonObject<Carcharge> findListByPage(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pageCount) {
        PageInfo<Carcharge> pageInfo = carchargeService.findListByPage(page, pageCount);
        return new JsonObject<>(0, "ok", pageInfo.getTotal(), pageInfo.getList());
    }


    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public R findById(@PathVariable Integer id) { // 确保使用 Integer
        Carcharge carcharge = carchargeService.findById(id);
        if (carcharge != null) {
            return R.ok(carcharge);
        }
        return R.fail("未找到对应记录");
    }
}
